//
//  SelectCityViewController.m
//  cityStar
//
//  Created by joyban on 15-1-5.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "SelectCityViewController.h"
#import "CItyCell.h"

#import "LoveCityViewController.h"
#import "UIColor+FlatUI.h"
#import "selectProvince.h"

#import "selectCityArrayModel.h"
#import "selectCityValueModel.h"
#include "HTDefine.h"

NSString *const CityCellID = @"CityCellID";


@interface SelectCityViewController ()

@property (weak, nonatomic) IBOutlet UITableView *SelectCityTable;
@property (nonatomic,strong) NSMutableArray *cellArray;
@property (nonatomic,strong) NSMutableArray *MycellArray;
@property (nonatomic,strong) CItyCell *SectionCell;
@property (nonatomic,strong) selectCityArrayModel *cityModel;

@end

@implementation SelectCityViewController

@synthesize selectCitys,CityArray;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.MycellArray = [[NSMutableArray alloc]init];
    self.cellArray = [[NSMutableArray alloc]init];
    selectCitys = @"杭州";
//    CityArray = [selectProvince shardProvinceInfo].ProvinceNameArray;

    CityArray = [[NSMutableArray alloc]initWithObjects:@"杭州",@"大连市",@"台州",@"宁波",@"绍兴",@"温州",@"桂林",@"西安",@"无锡", nil];
    self.navigationItem.title = @"选择所在地";
    [self addBackButton];
    __weak typeof(self)weakSelf = self;
    [self addRightButtonWithTitle:@"偏好城市" actionBlock:^{
        LoveCityViewController *loveCityViewController = [[LoveCityViewController alloc]init];
        [weakSelf.navigationController pushViewController:loveCityViewController animated:YES];
    }];
    [self setUpTableViews];
    [self webRequest_GetCityData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addRightButtonWithTitle:(NSString *)title actionBlock:(void(^)(void))block
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(0, 0, 65, 20)];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightButton setTitle:title forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor colorWithRed:0.980 green:0.450 blue:0.239 alpha:1.000] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(rightBar_Touched:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.rightBarActionBlock = block;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

-(void)setUpTableViews
{
    [self.SelectCityTable registerNib:[CItyCell nib] forCellReuseIdentifier:CityCellID];
}

-(void)webRequest_GetCityData
{
//    NSString *postUrl = [NSString stringWithFormat:@"%@",KInterface_CityList];
    [HttpRequestManager getMethod:KInterface_CityList parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        NSLog(@"%@",response);
        if (success) {
            self.cellArray = response[@"value"];
            [self.SelectCityTable reloadData];
        }else{
            
        }
    }];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.cellArray.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
    {
        return 1;
    }else{
        selectCityArrayModel *model = [selectCityArrayModel objectFromDictionary:self.cellArray[section - 1]];
        return model.city.count;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 25;
    }else if(section == 1)
    {
        return 43;
    }else{
        return 18;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [CItyCell heightOfTableCell];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section <= 1)
    {
        UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)];
        UILabel * label = [[UILabel alloc] init] ;
        label.frame = CGRectMake(15, 0, 200, 25);
        label.backgroundColor = [UIColor clearColor];
        label.font=[UIFont boldSystemFontOfSize:13];
        if(section == 0)
        {
            label.text = @"当前定位城市";
        }else
        {
            label.text = @"选择当前城市";
            UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 25, tableView.bounds.size.width, 18)];
            backView.backgroundColor = [UIColor colorFromHexCode:@"ffffff"];
            [sectionView addSubview:backView];
            UILabel * label2 = [[UILabel alloc] init];
            
            UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 17, tableView.bounds.size.width, 1)];
            [line setBackgroundColor:[UIColor colorFromHexCode:@"FEF3E2"]];
            [backView addSubview:line];
            
            label2.frame = CGRectMake(15, 0, 200, 18);
            label2.backgroundColor = [UIColor clearColor];
            label2.font=[UIFont boldSystemFontOfSize:11];
            label2.textColor = [UIColor colorFromHexCode:@"5F5B56"];
            label2.textAlignment = NSTextAlignmentLeft;
            selectCityArrayModel *model = [selectCityArrayModel objectFromDictionary:self.cellArray[section - 1]];
            label2.text = model.first_letter;
            [backView addSubview:label2];
        }
        label.textColor = [UIColor colorFromHexCode:@"5F5B56"];
        label.textAlignment = NSTextAlignmentLeft;
        [sectionView addSubview:label];
        [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"FEF3E2"]];
        return sectionView;
    }else{
        UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)];
        [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"ffffff"]];
        UILabel * label2 = [[UILabel alloc] init] ;
        label2.frame = CGRectMake(15, 0, 200, 18);
        label2.backgroundColor = [UIColor clearColor];
        label2.font=[UIFont boldSystemFontOfSize:11];
        label2.textColor = [UIColor colorFromHexCode:@"5F5B56"];
        label2.textAlignment = NSTextAlignmentLeft;
        UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 17, tableView.bounds.size.width, 1)];
        [line setBackgroundColor:[UIColor colorFromHexCode:@"FEF3E2"]];
        [sectionView addSubview:line];
        selectCityArrayModel *model = [selectCityArrayModel objectFromDictionary:self.cellArray[section - 1]];
        label2.text = model.first_letter;
        [sectionView addSubview:label2];
        return sectionView;

    }
}

#pragma mark - UITableViewDelegate and UITableViewDataSource

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CItyCell *cityCell = [tableView dequeueReusableCellWithIdentifier:CityCellID];
    cityCell.selectionStyle = UITableViewCellSelectionStyleGray;
    if(indexPath.section == 0)
    {
        self.SectionCell = cityCell;
        cityCell.NameLabel.text = selectCitys;
        cityCell.lineImage.hidden = YES;
    }else{
        selectCityArrayModel *model = [selectCityArrayModel objectFromDictionary:self.cellArray[indexPath.section - 1]];
        NSLog(@"%@",model.city);
        selectCityValueModel *cityModel = model.city[indexPath.row];
        cityCell.NameLabel.text = cityModel.name;
        cityCell.tag = [cityModel.city_id intValue];
        if(indexPath.row == model.city.count - 1)
        {
            cityCell.lineLift.constant = 0;
        }
        [self.MycellArray addObject:cityCell];
    }
    return cityCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
//    if(indexPath.section == 0)
//    {
//        for (int i = 0; i<self.cellArray.count; i ++) {
//            CItyCell *cell = self.MycellArray[i];
//            cell.lineImage.hidden = NO;
//        }
//    }else{
//        for (int i = 0; i<self.cellArray.count; i ++) {
//            CItyCell *cell = self.MycellArray[i];
//            if( i == indexPath.row && indexPath.row > 0)
//            {
//                cell.lineImage.hidden = YES ;
//                CItyCell *cellTwo = self.MycellArray[i-1];
//                cellTwo.lineImage.hidden = YES ;
//            }else if( indexPath.row == 0 && i == indexPath.row){
//                cell.lineImage.hidden = YES ;
//            }else{
//                cell.lineImage.hidden = NO;
//            }
////            cell.NameLabel.textColor = [UIColor colorFromHexCode:@"000000"];
//        }
//    }
    if (indexPath.section == 0) {
        [self.navigationController popToRootViewControllerAnimated:YES];
        return;
    }
    
    CItyCell *cityCell = (CItyCell *)[tableView cellForRowAtIndexPath:indexPath];
//    cityCell.NameLabel.textColor = [UIColor colorFromHexCode:@"ffffff"];
    [KStandUserDefaults setObject:[NSString stringWithFormat:@"%li",(long)cityCell.tag] forKey:KUserDefaultKey_PersonalCityID];
    [KStandUserDefaults setObject:[NSString stringWithFormat:@"%@",cityCell.NameLabel.text] forKey:KUserDefaultKey_PersonalCityName];

    [KStandUserDefaults synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showAlertView" object:cityCell.NameLabel.text];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
